using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Sale;


namespace EntLibEC.Dac.Sale
{
	/// <summary>
	/// Summary description for SaleGiftRelateMoneyDac.
	/// </summary>
	public class SaleGiftRelateMoneyDac
	{
		
		public SaleGiftRelateMoneyDac()
		{
		}

		public int Insert(SaleGiftRelateMoneyInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Sale_GiftRelateMoney
                            (
                            CreateUserSysNo, CreateTime, GiftDesc, 
                            ProductSysNo, MoneyStart, MoneyEnd, Status
                            )
                            VALUES (
                            @CreateUserSysNo, @CreateTime, @GiftDesc, 
                            @ProductSysNo, @MoneyStart, @MoneyEnd, @Status
                            );set @SysNo = SCOPE_IDENTITY();";

			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramCreateUserSysNo = new SqlParameter("@CreateUserSysNo", SqlDbType.Int,4);
			SqlParameter paramCreateTime = new SqlParameter("@CreateTime", SqlDbType.DateTime);
			SqlParameter paramGiftDesc = new SqlParameter("@GiftDesc", SqlDbType.NVarChar,200);
			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramMoneyStart = new SqlParameter("@MoneyStart", SqlDbType.Decimal,9);
			SqlParameter paramMoneyEnd = new SqlParameter("@MoneyEnd", SqlDbType.Decimal,9);
			SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int,4);

			paramSysNo.Direction = ParameterDirection.Output;

			if ( oParam.CreateUserSysNo != AppConst.IntNull)
				paramCreateUserSysNo.Value = oParam.CreateUserSysNo;
			else
				paramCreateUserSysNo.Value = System.DBNull.Value;
			if ( oParam.CreateTime != AppConst.DateTimeNull)
				paramCreateTime.Value = oParam.CreateTime;
			else
				paramCreateTime.Value = System.DBNull.Value;
			if ( oParam.GiftDesc != AppConst.StringNull)
				paramGiftDesc.Value = oParam.GiftDesc;
			else
				paramGiftDesc.Value = System.DBNull.Value;
			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.MoneyStart != AppConst.DecimalNull)
				paramMoneyStart.Value = oParam.MoneyStart;
			else
				paramMoneyStart.Value = System.DBNull.Value;
			if ( oParam.MoneyEnd != AppConst.DecimalNull)
				paramMoneyEnd.Value = oParam.MoneyEnd;
			else
				paramMoneyEnd.Value = System.DBNull.Value;
			if ( oParam.Status != AppConst.IntNull)
				paramStatus.Value = oParam.Status;
			else
				paramStatus.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramCreateUserSysNo);
			cmd.Parameters.Add(paramCreateTime);
			cmd.Parameters.Add(paramGiftDesc);
			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramMoneyStart);
			cmd.Parameters.Add(paramMoneyEnd);
			cmd.Parameters.Add(paramStatus);

			return SqlHelper.ExecuteNonQuery(cmd, out oParam.SysNo);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Ent_Sale_GiftRelateMoney SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int)
					{
						if ( (int)item == AppConst.IntNull)
							sb.Append(key).Append("= null");
						else
							sb.Append(key).Append("=").Append(item.ToString());
					}
					else if ( item is decimal )
					{
						if ( (decimal)item == AppConst.DecimalNull )
							sb.Append(key).Append("= null");
						else
							sb.Append(key).Append("=").Append(item.ToString());
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SysNo=").Append(paramHash["SysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}

		public int Delete(int sysno)
		{
			string sql = "delete from Ent_Sale_GiftRelateMoney where sysno = " + sysno;

			SqlCommand cmd = new SqlCommand(sql);
			return SqlHelper.ExecuteNonQuery(cmd);

		}
	}
}
